using System;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.SqlClient;

namespace ASPNETVN.PORTAL.Modules.CMS.Components
{
    public partial class Item
    {
        public static DataSet SelectRelationItems(int id)
        {
            const string spName = "p_CMS_Item_SelectRelationItems";
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand(spName);
            db.AddInParameter(dbCommand, "@ItemID", DbType.Int32, id);
            return db.ExecuteDataSet(dbCommand);
        }

        public static DataSet SelectBy_ModuleID(int moduleID)
        {
            const string spName = "p_CMS_Item_SelectBy_ModuleID";
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand(spName);
            db.AddInParameter(dbCommand, "@ModuleID", DbType.Guid, moduleID);
            return db.ExecuteDataSet(dbCommand);
        }

        public static DataSet SelectBy_ModuleID_Status(int moduleID, string status)
        {
            const string spName = "p_CMS_Item_SelectBy_ModuleID_Status";
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand(spName);
            db.AddInParameter(dbCommand, "@ModuleID", DbType.Int32, moduleID);
            db.AddInParameter(dbCommand, "@Status", DbType.String, status);
            return db.ExecuteDataSet(dbCommand);
        }

        public static int IncreaseTotalViews(int itemID)
        {
            const string spName = "p_CMS_Item_IncreaseTotalViews";
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand(spName);
            db.AddInParameter(dbCommand, "@ItemID", DbType.Int32, itemID);
            return db.ExecuteNonQuery(dbCommand);
        }

        public static int IncreaseTotalComments(int itemID)
        {
            const string spName = "p_CMS_Item_IncreaseTotalComments";
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand(spName);
            db.AddInParameter(dbCommand, "@ItemID", DbType.Int32, itemID);
            return db.ExecuteNonQuery(dbCommand);
        }

        public int Approve()
        {
            const string spName = "[dbo].[p_CMS_Item_Approve]";
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand(spName);
            db.AddInParameter(dbCommand, "@ID", DbType.Int32, ID);
            db.AddInParameter(dbCommand, "@ApprovedBy", DbType.String, ApprovedBy);
            db.AddInParameter(dbCommand, "@ApprovedDate", DbType.DateTime, ApprovedDate);
            db.AddInParameter(dbCommand, "@PublishedDate", DbType.DateTime, PublishedDate);
            db.AddInParameter(dbCommand, "@ExpiredDate", DbType.DateTime, ExpiredDate);
            return db.ExecuteNonQuery(dbCommand);
        }

        /// <summary>
        /// Get top items
        /// </summary>
        /// <param name="moduleID">Module ID</param>
        /// <param name="optionView">TopView | TopComment | None</param>
        /// <returns>Dataset</returns>
        public static DataSet GetTopItems(Guid moduleID, string optionView)
        {
            const string spName = "[dbo].[p_CMS_Item_GetTopItems]";
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand(spName);
            db.AddInParameter(dbCommand, "@ModuleID", DbType.Guid, moduleID);
            db.AddInParameter(dbCommand, "@OptionView", DbType.String, optionView);

            return db.ExecuteDataSet(dbCommand);
        }

        /// <summary>
        /// Get all items by CategoryID
        /// </summary>
        /// <param name="categoryID">CategoryID can be root ID or child ID</param>
        /// <returns>DataSet</returns>
        public static DataSet GetItemsByCategoryID(int categoryID)
        {
            const string spName = "[dbo].[p_CMS_Item_GetItemsByCategoryID]";
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand(spName);
            db.AddInParameter(dbCommand, "@CategoryID", DbType.Int32, categoryID);

            return db.ExecuteDataSet(dbCommand);
        }

        public static DataSet GetItemsByDisplayType(Guid moduleID, string displayType)
        {
            const string spName = "[dbo].[p_CMS_Item_GetItemsByDisplayType]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);
            db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, moduleID);
            db.AddInParameter(dbCommand, "@DisplayType", SqlDbType.VarChar, displayType);

            return db.ExecuteDataSet(dbCommand);
        }

        //Yen le lam
        public static DataSet GetItemsByDisplayType(Guid moduleID, int categoryID, string displayType)
        {
            const string spName = "[dbo].[p_CMS_Item_GetItemsByCategoryID_DisplayType]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);
            db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, moduleID);
            db.AddInParameter(dbCommand, "@CategoryID", SqlDbType.Int, categoryID);
            db.AddInParameter(dbCommand, "@DisplayType", SqlDbType.VarChar, displayType);

            return db.ExecuteDataSet(dbCommand);
        }
        public static void SetupDisplayItem(int itemID, string displayType)
        {
            const string spName = "[dbo].[p_CMS_Item_SetupDisplayItem]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);
            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, itemID);
            db.AddInParameter(dbCommand, "@DisplayType", SqlDbType.VarChar, displayType);

            db.ExecuteNonQuery(dbCommand);
        }

        public static DataSet GetItemsByTitle(string title)
        {
            const string spName = "[dbo].[p_CMS_Item_GetItemsByTitle]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);
            db.AddInParameter(dbCommand, "@Title", SqlDbType.NVarChar, title);

            return db.ExecuteDataSet(dbCommand);
        }
    }
}